package com.kye.express.business.personal_setting;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.business.personal_setting.modify_name.ModifyNameOrCompanyActivity;
import com.kye.express.databinding.ActivityPersonalSettingBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.model.bean.user.User;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import java.util.Calendar;

import io.reactivex.functions.Consumer;

public class PersonalSettingActivity extends BaseAppCompatActivity implements PersonalSettingContract.PersonalSettingView {

    private ActivityPersonalSettingBinding mBinding;
    private PersonalSettingContract.PersonalSettingPresenter mPresenter;
    private Dialog mDialog;
    private String[] mGenderArray;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_personal_setting);

        initEvent();
        initData();
    }

    private void initEvent() {
        setSupportActionBar(mBinding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        mBinding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        RxView.clicks(mBinding.linearLayoutName)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ModifyNameOrCompanyActivity.startActivity(PersonalSettingActivity.this, ModifyNameOrCompanyActivity.KEY_NAME);
                    }
                });

        RxView.clicks(mBinding.linearLayoutGender)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {

                    private MaterialDialog.Builder mDialog;

                    @Override
                    public void accept(Object o) throws Exception {
                        new MaterialDialog.Builder(PersonalSettingActivity.this)
                                .items(mGenderArray)
                                .itemsCallback(new MaterialDialog.ListCallback() {
                                    @Override
                                    public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                                        mPresenter.editPersonalSetting(mBinding.getUser(), text.toString().trim());

                                        dialog.dismiss();
                                    }
                                }).show();
                    }
                });

        RxView.clicks(mBinding.linearLayoutCompany)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ModifyNameOrCompanyActivity.startActivity(PersonalSettingActivity.this, ModifyNameOrCompanyActivity.KEY_COMPANY);
                    }
                });

        RxView.clicks(mBinding.linearLayoutBirthday)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {

                    public static final String DATE_PICKER_DIALOG = "DATE_PICKER_DIALOG";

                    @Override
                    public void accept(Object o) throws Exception {
                        if (mBinding.getUser().getBirthday() != null) {
                            ToastManager.show(PersonalSettingActivity.this, R.string.personal_setting_cant_edit_birthday);
                            return;
                        }

                        int year;
                        int monthOfYear;
                        int dayOfMonth;

                        Calendar calendar = Calendar.getInstance();
                        if (mBinding.getUser().getBirthday() != null) {
                            calendar.setTime(mBinding.getUser().getBirthday());
                        }

                        year = calendar.get(Calendar.YEAR);
                        monthOfYear = calendar.get(Calendar.MONTH);
                        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

                        DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
                                new DatePickerDialog.OnDateSetListener() {
                                    @Override
                                    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                                        Calendar calendar = Calendar.getInstance();
                                        calendar.set(year, monthOfYear, dayOfMonth);
                                        mPresenter.editPersonalSetting(mBinding.getUser(), calendar.getTime());

                                        view.dismiss();
                                    }
                                },
                                year,
                                monthOfYear,
                                dayOfMonth
                        );
                        datePickerDialog.vibrate(true);
                        datePickerDialog.setVersion(DatePickerDialog.Version.VERSION_1);
                        datePickerDialog.showYearPickerFirst(true);
                        datePickerDialog.show(getFragmentManager(), DATE_PICKER_DIALOG);
                    }
                });

        RxView.clicks(mBinding.buttonLogout)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mPresenter.logout();
                    }
                });
    }

    private void initData() {
        mPresenter = new PersonalSettingContract.PersonalSettingPresenter();
        mPresenter.bind(this);

        mGenderArray = getResources().getStringArray(R.array.personal_setting_gender);
    }

    @Override
    public void onUser(User user) {
        mBinding.setUser(user);
    }

    @Override
    public void onIsPush(Boolean isPush) {
        mBinding.setIsPush(isPush);
    }

    @Override
    public void onLogout() {
        finish();
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onLoading() {
        if (mDialog == null) {
            mDialog = new KyeProgressDialog.Builder(this)
                    .build();
        }
        mDialog.show();
    }

    @Override
    public void onLoaded() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
}
